package com.jic.member.controller.manage;

import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberIntegralBase;
import com.jic.member.enums.EXCEPTION;
import com.jic.member.request.MememberIntegralBaseQueryRequest;
import com.jic.member.request.MememberIntegralBaseRequest;
import com.jic.member.request.MememberIntegralBaseUpdateRequest;
import com.jic.member.response.MememberIntegralBaseResponse;
import com.jic.member.service.MememberIntegralBaseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ymh
 * @date 2020-04-02
 * 会员积分基础表服务
 */

@RestController
@RequestMapping("/integralManage")
public class MememberIntegralBaseController {

    private final MememberIntegralBaseService mememberIntegralBaseService;


    public MememberIntegralBaseController(MememberIntegralBaseService mememberIntegralBaseService) {
        this.mememberIntegralBaseService = mememberIntegralBaseService;
    }


    /**
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "获取积分基础信息列表")
    @ResponseBody
    @RequestMapping(value = "/queryMemberBaseList")
    public RestResult<PageResult<MememberIntegralBaseResponse>> queryMemberBaseList(@RequestBody MememberIntegralBaseQueryRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        MememberIntegralBase mememberIntegralBase = BeanUtil.copy(form, MememberIntegralBase.class);
        PageResult<MememberIntegralBase> result = mememberIntegralBaseService.queryPage(mememberIntegralBase, page);
        List<MememberIntegralBaseResponse> list = result.getRows().stream().map(temp -> {
            MememberIntegralBaseResponse mememberIntegralBaseResponse = BeanUtil.copy(temp, MememberIntegralBaseResponse.class);
            return mememberIntegralBaseResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MememberIntegralBaseResponse> pageResult = new PageResult<>();
        pageResult.setRows(list);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }



    @ApiOperation("新增用户会员积分管理/基础设置")
    @PostMapping("/addIntegralBaseInfo")
    @ResponseBody
    public RestResult addIntegralBaseInfo(@RequestBody MememberIntegralBaseRequest mememberIntegralBaseRequest){
        final RestResult restResult = mememberIntegralBaseService.insertIntegralBaseInfo(mememberIntegralBaseRequest);
        return restResult;

    }

    @ApiOperation("新增会员积分管理/基础设置")
    @PostMapping("/addStoreIntegralBaseInfo")
    @ResponseBody
    public RestResult addStoreIntegralBaseInfo(@RequestBody MememberIntegralBaseRequest mememberIntegralBaseRequest){
        final RestResult restResult = mememberIntegralBaseService.insertStoreIntegralBaseInfo(mememberIntegralBaseRequest);
        return restResult;

    }


    @ApiOperation("删除基础设置")
    @PostMapping("/updateBase")
    @ResponseBody
    public RestResult updateBase(@RequestBody MememberIntegralBaseUpdateRequest mememberIntegralBaseUpdateRequest){
        RestResult restResult = new RestResult();
        final long delete = mememberIntegralBaseService.delete(mememberIntegralBaseUpdateRequest.getId());
        if(delete> 0 ){
            restResult.setCode("0000");
        }else {
            restResult.setCode(EXCEPTION.FAIL.getName());
        }
        return restResult;

    }
    
    @ApiOperation("查询基础信息")
    @PostMapping("/queryBase")
    @ResponseBody
    public RestResult queryBase(@RequestBody MememberIntegralBaseQueryRequest mememberIntegralBaseQueryRequest){
        return mememberIntegralBaseService.queryByDeptId(mememberIntegralBaseQueryRequest);

    }

    /**
     *
     * @param form
     * @return
     */
    @ApiOperation(value = "获取子平台积分基础信息列表")
    @ResponseBody
    @RequestMapping(value = "/queryChildMemberBaseList")
    public RestResult<PageResult<MememberIntegralBaseResponse>> queryChildMemberBaseList(@RequestBody MememberIntegralBaseQueryRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        PageResult<MememberIntegralBase> result = mememberIntegralBaseService.queryPageForChild(form, page);
        List<MememberIntegralBaseResponse> list = result.getRows().stream().map(temp -> {
            MememberIntegralBaseResponse mememberIntegralBaseResponse = BeanUtil.copy(temp, MememberIntegralBaseResponse.class);
            return mememberIntegralBaseResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<MememberIntegralBaseResponse> pageResult = new PageResult<>();
        pageResult.setRows(list);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }


}

